﻿using System;
using System.Collections.Generic;
using Logging;
using System.IO;
using System.Linq;
using SelectionAddin.Extension.Test;
//using Microsoft.VisualStudio.Profiler;

namespace CompleterCLI
{
    class Program
    {
        static void Main(string[] args)
        {
            /*DataCollection.StopProfile(
                Microsoft.VisualStudio.Profiler.ProfileLevel.Global,
                DataCollection.CurrentId);*/
            string logFile = "s:\\log\\" + "CompleterMain"
                    + "." + DateTime.Now.ToString("s").Replace(":", "")
                    + ".txt";
            Logger.SetOutputFile(File.Open(logFile, FileMode.Create));

            string codeFile = File.OpenText(@"C:\Users\perelman\Documents\Visual Studio 2010\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs").ReadToEnd();
            string codeLiteral = @"
                    using System;
                    using System.Collections.Generic;
                    using System.Linq;
                    using System.Xml;
    
                    namespace HelloWorld
                    {
                        class Program
                        {
                            static void Main(string[] args)
                            {
                                string s = ""foo"", t = ""bar"";
                                int i = 42;
                                bool b = xx;
                                Program p = xx;
                                IntList l = null;
                                XmlReader res = xx;

                                DateTime start = DateTime.Now;
                                DateTime end = DateTime.Now;
                                var diff = /*^*/start - xx/*$*/
                            }

                            public static void TakesIEnumObject(IEnumerable<object> xs) { }

                            public string Fontname { get { ""name""; } }
                            public string Filename { get { ""name""; } }
                            public void UseFile(string filename) { }
                            public void UseFont(string fontname) { }

                            public void TestCase()
                            {
                                UseFile(Filename);
                                UseFont(Fontname);
                            }
                        }
                        class IntList : List<int> {}
                    }";
            //string code = codeFile;
            string code = codeLiteral;
            Logger.PrintHeader("Code");
            Logger.PrintLine(code);

            var test = new CompleterRankBoundTestInstance(
                        "HelloWorld", code, null, default(int))
                            .Test;

            Logger.PrintHeader("Query");
            Logger.PrintLine(test.GetRewriteString());
            Logger.PrintLine("Type: " + test.GetTypeString());

            Logger.PrintHeader("Results");
            foreach (string result in test.GetResultStrings().Take(5))
            {
                Logger.PrintLine(result);
            }

            /*
            const int num = 5;

            DateTime start = DateTime.Now;
            var results = new CompleterRankBoundTestInstance("HelloWorld", code, null, num)
                .Test.GetResultStrings();
            var resultsList = results.Take(num).ToList();
            DateTime end = DateTime.Now;
            TimeSpan timeForNum = end - start;
            Logger.PrintLine("Time to compute " + resultsList.Count + " results: " + timeForNum);

            DataCollection.StartProfile(
                Microsoft.VisualStudio.Profiler.ProfileLevel.Global,
                DataCollection.CurrentId);
            DateTime start2 = DateTime.Now;
            var results2 = new CompleterRankBoundTestInstance("HelloWorld", code, null, num)
                .Test.GetResultStrings();
            var results2List = results2.Take(num).ToList();
            DateTime end2 = DateTime.Now;
            DataCollection.StopProfile(
                Microsoft.VisualStudio.Profiler.ProfileLevel.Global,
                DataCollection.CurrentId);
            TimeSpan timeForNum2 = end2 - start2;
            Logger.PrintLine("Time to recompute " + results2List.Count + " results: " + timeForNum2);

            //Logger.PrintLine(string.Join("\n", results.Take(100).ToArray()));
            foreach(string result in resultsList) {
                Logger.PrintLine(result);
            }

            //Logger.PrintLine(SelectionAddin.Extension.AbstractTypes.AbstractTypeInferencer.externalTermMeanings.Status.ToString());
             */
        }
    }
}
